#coding=utf-8
from config import *



# 以一个坐标为中心绘制一个精灵，可以偏移
def fixed_point_draw_sprite(source_Surface, target_Surface, center_coordinate, skewing_vector=(0,0)):
    x = center_coordinate[0] - source_Surface.get_width() / 2 + skewing_vector[0]
    y = center_coordinate[1] - source_Surface.get_height() / 2 + skewing_vector[1]
    target_Surface.blit(source_Surface,(x, y))

# 以一个坐标为左上角绘制出一个精灵，可以偏移
def draw_sprite(self, source_Surface, target_Surface,skewing_vector=(0,0)):
    target_Surface.blit(source_Surface,(self.x + skewing_vector[0], self.y + skewing_vector[1]))

# 自定义排序方式
def on_off_value_sort_mode(a, b):
    if a == 0:
        return 1
    if b == 0:
        return -1
    if a > b:
        result = 1
    elif a < b:
        result = -1
    else:
        assert a == b
        result = 0
    return result

# 包装自定义排序函数
def cmp_to_key(mycmp):
    '将cmp = def 转换为 key = function'

    class K(object):
        def __init__(self, obj, *args):
            assert type(obj) in [list,set,tuple],'比较对象必须为元祖或列表或集合'
            self.obj = obj[1]

        def __lt__(self, other):
            return mycmp(self.obj, other.obj) < 0

        def __gt__(self, other):
            return mycmp(self.obj, other.obj) > 0

        def __eq__(self, other):
            return mycmp(self.obj, other.obj) == 0

        def __le__(self, other):
            return mycmp(self.obj, other.obj) <= 0

        def __ge__(self, other):
            return mycmp(self.obj, other.obj) >= 0

        def __ne__(self, other):
            return mycmp(self.obj, other.obj) != 0
    return K

# 获取行动状态
def get_action_status(control):
    '''传入一个控制器(字典)，获取开关值最小的行动(最新按下的动作键）,
    无生效行动返回False，有就返回对应动作名'''
    kv_tuple = sorted(control.items(),key=cmp_to_key(on_off_value_sort_mode)) # 排序，索引0为最小
    # print(kv_tuple)-----[('up', 0), ('down', 0), ('left', 0), ('right', 0), ('gj', 0)]
    if kv_tuple[0][1] == 0:
        return False
    else:
        action = ''
        # 索引2开关值不为0的话，则尝试判定为左下、右下、左上、右上这样的方向, 如果1、2的方向不为对立方向，则不操作
        if kv_tuple[1][1] != 0:
            if (kv_tuple[0][0],kv_tuple[1][0]) in [(UP,LEFT),(LEFT,UP)]:
                action = LEFT + '_' + UP
            elif (kv_tuple[0][0],kv_tuple[1][0]) in [(UP,RIGHT),(RIGHT,UP)]:
                action = RIGHT + '_' + UP
            elif (kv_tuple[0][0],kv_tuple[1][0]) in [(DOWN,LEFT),(LEFT,DOWN)]:
                action = LEFT + '_' + DOWN
            elif (kv_tuple[0][0],kv_tuple[1][0]) in [(DOWN,RIGHT),(RIGHT,DOWN)]:
                action = RIGHT + '_' + DOWN

        # 如果上述代码未操作，则赋值为索引为1的动作
        if action == '':
            action = kv_tuple[0][0]
        return action


from copy import deepcopy
old_value = None
# 只有值发生改变时，才打印
def change_print(param):
    global old_value
    if old_value != param:
        old_value = deepcopy(param)
        print(param)